from django.shortcuts import render,get_object_or_404
from django.http import HttpResponse,Http404,HttpResponseRedirect
from django.core.urlresolvers import reverse
from polls.models import Poll,Choice

# Create your views here.

def index(request):
    latest_poll_set = Poll.objects.order_by('-pub_date')[:5]
    #output = ", ".join([p.question for p in latest_poll_set])
    #return HttpResponse("hello world. in poll index")    
    #return HttpResponse(output)
    context = {'latest_poll_set': latest_poll_set}
    return render(request, 'polls/index.html', context)

def detail(request, poll_id):
    #try:
        #poll = Poll.objects.get(pk=poll_id)
    #except Poll.DoesNotExist:
        #raise Http404
    poll = get_object_or_404(Poll, pk=poll_id)
    return render(request, 'polls/detail.html', {'poll': poll})

def results(request, poll_id):
    #return HttpResponse("You're looking at the results of poll %s" % poll_id)
    poll = get_object_or_404(Poll, pk=poll_id)
    return render(request, 'polls/results.html', {'poll': poll})

def vote(request, poll_id):
    #return HttpResponse("You're voting on poll %s" % poll_id)
    p = get_object_or_404(Poll, pk=poll_id)
    try:
        select_choice = p.choice_set.get(pk=request.POST['choice'])
    except (KeyError, Choice.DoesNotExist):
        return render(request, 'polls/detail.html', {
            'poll': p,
            'error_message': "You didn't select a choice."
        })        
    else:
        select_choice.votes += 1
        select_choice.save()
        return HttpResponseRedirect(reverse('polls:results', args=(p.id,)))